<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login_model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Identity_model', 'id');
    }


    /**
     * 用户登陆
     * @param string $username
     * @param string $password
     * @param string $captcha_code 验证码
     * @param int $user_type
     * @return array
     */
    public function do_login($username, $password, $captcha_code, $user_type)
    {
        
        //验证码校验 (后台用户、超级管理员)
        if (($user_type == 2 || $user_type == 3) && ($captcha_code != $this->session->captcha_code)) {
            $this->verify_error['message'] = '验证码错误';
            return $this->verify_error;
        }
        //用户名密码校验
        $result = $this->id->do_auth($username, $password, $user_type);
        if ($result != false) {
            // 登陆成功
            switch ($user_type) {
                case 1://注册用户
                    $session_data = [
                        'f_uid' => $result['id'],
                        'user_name' => $result['user_name'],
                        'real_user' => $result['real_name'],
                        'user_type' => $user_type// 1=注册用户
                    ];
                    break;
                case 2://管理员
                    $session_data = [
                        'm_uid' => $result['id'],
                        'gid' => $result['group_id'],
                        'username' => $result['user_name'],
                        'real_name' => $result['real_name'],
                        'user_type' => $user_type,// 2=管理员
                        'menu' => $this->get_menu($result['group_id'])
                    ];
                    break;
                case 3://超级管理员
                    $session_data = [
                        'm_uid' => $result['id'],
                        'username' => $result['user_name'],
                        'real_name' => $result['real_name'],
                        'user_type' => $user_type,// 3=超级管理员
                        'menu' => $this->get_menu(1),
                    ];
                    break;
                default ://登陆失败
                    $this->verify_error['message'] = '用户名或密码错误';
                    return $this->verify_error;
            }
            // 设置登陆session
           
            $this->session->set_userdata($session_data);
            // 记录登陆日志
            $this->write_login_log($user_type, 1);
            $this->success['data'] = $session_data;
            return $this->success;
        }

        // 登陆失败
        $this->verify_error['message'] = '用户名或密码错误';
        return $this->verify_error;
    }


    /**
     * 注销登陆
     */
    public function logout()
    {
        $this->id->del_auth();
    }



    /**
     * 获取用户菜单
     * @param int $gid 用户组id
     * @return array
     */
    protected function get_menu($gid)
    {
        $this->load->model('system_group_menu_model');
        return $this->system_group_menu_model->get_user_menu_by_gid($gid);
        
    }


    /**
     * 记录登陆日志
     * @param int $user_type [1:注册用户, 2:后台管理用户, 3:超级管理员]
     * @param int $login_type [0:手机端, 1:网页端]
     */
    protected function write_login_log($user_type, $login_type)
    {
        $this->load->helper(array("public"));
        if ($user_type == '1') {
            $uid = $this->session->f_uid;
        } else {
            $uid = $this->session->m_uid;
        }
        // 记录登陆日志信息
        $login_info = array(
            "uid" => $uid,
            "ip" => get_ip(),
            "time" => time(),
            'user_type' => $user_type,
            "login_type" => $login_type
        );
        $this->db->set($login_info)->insert("system_login_log");
    }

    /**
     * 获取登录日志信息
     * @param unknown $uid 用户ID
     */
    function get_login_log_id($uid)
    {
        $this->db->where('uid', $uid);
        $this->db->limit(5);//取出五条数据
        $this->db->order_by('time', 'desc');//登录时间最新的
        return $this->db->get('login_log')->result_array();
    }
}